#!/usr/bin/env python
""" This is a CLI script for retrieving a person's id in the badges db given
their FAS username.
"""

import __main__
__main__.__requires__ = __requires__ = ["tahrir-api", "sqlalchemy>=0.7"];
import pkg_resources
pkg_resources.require(__requires__)

import argparse
import transaction
import sys

from tahrir_api.dbapi import TahrirDatabase

import fedmsg
import fedmsg.config

import fedbadges.utils


def parse_args():
    parser = argparse.ArgumentParser(__doc__)
    parser.add_argument('--user', default=None, help="A FAS username")
    args = parser.parse_args()
    if not args.user:
        print "You must specify a FAS username."
        sys.exit(1)
    return args


def initialize():
    fm_config = fedmsg.config.load_config()
    fm_config['cert_prefix'] = 'fedbadges'
    fm_config['name'] = 'relay_inbound'
    fm_config['active'] = True
    fedmsg.init(**fm_config)
    uri = fm_config['badges_global']['database_uri']
    tahrir = TahrirDatabase(
        uri,
        notification_callback=fedbadges.utils.notification_callback,
    )
    return tahrir


def main(tahrir, nickname):
    person = tahrir.get_person(nickname=nickname)

    if not person:
        print "No such person %r" % nickname
        sys.exit(1)

    print person.nickname, "has the id", person.id


if __name__ == '__main__':
    args = parse_args()
    tahrir = initialize()
    main(tahrir, args.user)
